﻿using TfsGiggle.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Diagnostics;
using System.Xml;
using System.Text;

namespace TfsGiggle.Core.UnitTest
{


    /// <summary>
    ///This is a test class for StatisticsIntegrationTest and is intended
    ///to contain all StatisticsIntegrationTest Unit Tests
    ///</summary>
    [TestClass()]
    public class StatisticsTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Instance
        ///</summary>
        [TestMethod()]
        public void AddingOneShouldReturnOne()
        {
            Statistics.Instance.Open("Statistics");
            Statistics.Instance.NumberOfUnitTests.Add(1);
            Assert.AreEqual(1, Statistics.Instance.NumberOfUnitTests.Value);
            Assert.IsTrue(Statistics.Instance.ToString().Contains("NumberOfUnitTests"), "Counter name must have been added");
            var document = new XmlDocument();
            document.LoadXml(Statistics.Instance.ToString());
            Assert.IsTrue(document.ChildNodes.Count > 0, "There must be nodes representing counters");
            Assert.AreEqual(1, Convert.ToInt32(document.SelectNodes("descendant::Counter").Item(0).Attributes[1].Value),"And the value must be 1");
            // The below gives Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot dynamically invoke method 'WriteLine' because it has a Conditional attribute
            // Debug.WriteLine(Statistics.Instance.ToString());
            var sb = new StringBuilder(Statistics.Instance.ToString());
            Debug.WriteLine(sb.ToString());
            Statistics.Instance.Dispose();
        }
    }
}
